<?


$sql['columns'] = 'link.*,category.name';
$sql['tables'] = array('link');
$sql['tables']['category'] = "LEFT JOIN `category` USING (category_id)";

$sql['wheres'][] = 'status_id = 1';
$sql['wheres'][] = "title != ''";
$sql['wheres'][] = "url != ''";

if (!empty($_GET['experimental']) && preg_match('/^[YN]$/',$_GET['experimental'])) {
	$sql['wheres'][] = "experimental = ".dbQuote($_GET['experimental']);
	$link = linktoself(array('delete'=>'experimental'));
	print "<div class=\"filter\">Where <b>experimental={$_GET['experimental']}</b>. [<a href=\"$link\">remove filter</a>]</div>";

}
if (!empty($_GET['category_id'])) {
	if ($name = getOne("SELECT name FROM `category` WHERE category_id = ".intval($_GET['category_id']))) {
		$sql['wheres'][] = "category_id = ".intval($_GET['category_id']);
		$link = linktoself(array('delete'=>'category_id'));
		print "<div class=\"filter\">In the <b>".he($name)."</b> category. [<a href=\"$link\">remove filter</a>]</div>";
	}

}
if (!empty($_GET['developer_id'])) {
	if ($name = getOne("SELECT name FROM `developer` WHERE developer_id = ".intval($_GET['developer_id']))) {
		$sql['wheres'][] = "developer_id = ".intval($_GET['developer_id']);
		$link = linktoself(array('delete'=>'developer_id'));
		print "<div class=\"filter\">By <b>".he($name)."</b>. [<a href=\"$link\">remove filter</a>]</div>";
	}

}
if (!empty($_GET['added_by'])) {
	if ($name = getOne("SELECT realname FROM `user` WHERE user_id = ".intval($_GET['added_by']))) {
		//todo - link to geograph profile?
		$sql['wheres'][] = "added_by = ".intval($_GET['added_by']);
		$link = linktoself(array('delete'=>'added_by'));
		print "<div class=\"filter\">Added by <b>".he($name)."</b>. [<a href=\"$link\">remove filter</a>]</div>";
	}

}
if (!empty($_GET['last_update_by'])) {
	if ($name = getOne("SELECT realname FROM `user` WHERE user_id = ".intval($_GET['last_update_by']))) {
		//todo - link to geograph profile?
		$sql['wheres'][] = "last_update_by = ".intval($_GET['last_update_by']);
		$link = linktoself(array('delete'=>'last_update_by'));
		print "<div class=\"filter\">Last Update by <b>".he($name)."</b>. [<a href=\"$link\">remove filter</a>]</div>";
	}

}
if (!empty($_GET['tag_id'])) {
	if ($row2 = getRow("SELECT tag_id,display FROM `tag` WHERE `tag_id` = ".dbQuote($_GET['tag_id']))) {
		$sql['tables'][] = "INNER JOIN `link2tag` USING (link_id)";
		$sql['wheres'][] = "link2tag.tag_id = ".$row2['tag_id'];
		$link = linktoself(array('delete'=>'tag_id'));
		print "<div class=\"filter\">Tagged with <b><span class=\"tag\"><span>".he($row2['display'])."</span></span></b>. [<a href=\"$link\">remove filter</a>]</div>";
	}
}
if (!empty($_GET['tag'])) {
	if ($row2 = getRow("SELECT tag_id,display FROM `tag` WHERE `tag` = ".dbQuote($_GET['tag']))) {
		$sql['tables'][] = "INNER JOIN `link2tag` USING (link_id)";
		$sql['wheres'][] = "link2tag.tag_id = ".$row2['tag_id'];
		$link = linktoself(array('delete'=>'tag'));
		print "<div class=\"filter\">Tagged with <b><span class=\"tag\"><span>".he($row2['display'])."</span></span></b>. [<a href=\"$link\">remove filter</a>]</div>";
	}
}
if (!empty($_GET['internal']) && preg_match('/^[YN]$/',$_GET['internal'])) {
	if (true) {
		$sql['wheres'][] = "internal = ".dbQuote($_GET['internal']);
	} else {	
		$row = getRow("SHOW FULL FIELDS FROM link LIKE 'sites'");

		preg_match("/^(\w+)\(?([^)]*)\)?/",$row['Type'],$m);
		$values = explode(',',$m[2]);
		$ors = array();
		foreach ($values as $key=>$val) {
			$ors[] = "url LIKE 'http://".trim($val,"'")."/%'";
		}
		$sql['wheres'][] = (empty($_GET['internal'])?'NOT ':'')."(".implode(' OR ',$ors).")";
	}
	
	$link = linktoself(array('delete'=>'internal'));
	print "<div class=\"filter\">".(($_GET['internal'] == 'N')?'Not ':'')."<b>Internal Geograph</b> Link. [<a href=\"$link\">remove filter</a>]</div>";
	
}
if (isset($_GET['recent'])) {
	
	$sql['wheres'][] = "introduced > DATE_SUB(NOW(),INTERVAL 30 DAY)";
	
	$link = linktoself(array('delete'=>'recent'));
	print "<div class=\"filter\">Introduced in the <b>last 30 days</b>. [<a href=\"$link\">remove filter</a>]</div>";
	
}

if (!empty($_GET['site']) && preg_match('/^[\w\.-]+$/',$_GET['site'])) {
	
	$sql['wheres'][] = "FIND_IN_SET(".dbQuote($_GET['site']).",sites) > 0";
		
	$link = linktoself(array('delete'=>'site'));
	print "<div class=\"filter\">Relevent to <b>".he($_GET['site'])."</b> [<a href=\"$link\">remove filter</a>]</div>";
	
}


if (!empty($_GET['dd'])) {
	print "<pre>";
	print_r($sql);
	print "</pre>";
}

if (!empty($_GET['q'])) {
	//TODO invoke sphinx... 
}

//TODO add an experimental filter?
//Experimental Features: [Include], Exclude or Show Only 